//
//  QRCodeScanningVC.m
//  SGQRCodeExample
//
//  Created by apple on 17/3/21.
//  Copyright © 2017年 JP_lee. All rights reserved.
//

#import "QRCodeScanningVC.h"

@interface QRCodeScanningVC () <UIAlertViewDelegate>
@end

@implementation QRCodeScanningVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 注册观察者
    [SGQRCodeNotificationCenter addObserver:self selector:@selector(SGQRCodeInformationFromeAibum:) name:SGQRCodeInformationFromeAibum object:nil];
    [SGQRCodeNotificationCenter addObserver:self selector:@selector(SGQRCodeInformationFromeScanning:) name:SGQRCodeInformationFromeScanning object:nil];
    
    UIButton *cancel = [UIButton buttonWithType:0];
    cancel.frame = CGRectMake(10, kStatusBarHeight, 44, 44);
    cancel.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [cancel setImage:NameImage(@"login_cancel") forState:UIControlStateNormal];
    cancel.imageEdgeInsets = UIEdgeInsetsMake(8, 8, 8, 8);
    [cancel addTarget:self action:@selector(dismissController) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancel];
}

- (void)SGQRCodeInformationFromeAibum:(NSNotification *)noti {
    // 相册
    [self clearScaning];
}

- (void)SGQRCodeInformationFromeScanning:(NSNotification *)noti {
    NSString *string = noti.object;
    
    [self pauseScaning];
    /*
    @nsm_weak(self)
    [_networker POST:string parameters:@{@"app": @"1"} success:^(NSMResponseObject *responseObject) {
        @nsm_strong(self)
        !self.didScanSucess?:self.didScanSucess(responseObject.data[@"sn"]);
        [self dismissController];
    } empty:^(NSMResponseObject *responseObject) {
        @nsm_strong(self)
        [self showAlert:@"识别失败,请重新扫描" cancel:^(UIAlertAction *action) {
            [self dismissController];
        } done:^(UIAlertAction *action) {
            [self resumeScaning];
        }];
    } failure:^(NSError *error) {
        @nsm_strong(self)
        [self showAlert:@"识别失败,请重新扫描" cancel:^(UIAlertAction *action) {
            [self dismissController];
        } done:^(UIAlertAction *action) {
            [self resumeScaning];
        }];
    }];
    return;
     */
    
    // 扫描结果
    NSString *prefix = @"qqj://";
    BOOL isID = [string hasPrefix:prefix];
    string = [string substringFromIndex:prefix.length];
    // 14位纯数字二维码
    BOOL isSN = (![string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]].length) && string.length==14;
    BOOL isOk = isID || isSN;
    if (isOk) {
        [self pauseScaning];
        @nsm_weak(self)
        [self dismissViewControllerAnimated:YES completion:^{
            @nsm_strong(self)
            !self.didScanSucess?:self.didScanSucess(string);
        }];
    } else {
        [self pauseScaning];
        [self showAlert:@"非识别二维码" action:^(UIAlertAction *action) {
            [self resumeScaning];
        }];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
            [self resumeScaning];
            break;
        case 1:
        {
            !_didScanSucess?:_didScanSucess(alertView.identifierEx);
            [self dismissController];
        }
            break;
        default:
            break;
    }
}

- (void)dismissController {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)dealloc {
    [SGQRCodeNotificationCenter removeObserver:self];
}

@end
